package leaf.science.ui.demoplayer
{
	import caurina.transitions.Tweener;
	
	import fl.video.FLVPlayback;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class DemosPlayer extends DemosPlayerSkin
	{
		
		private var _screen:Sprite;
		public var isOpen:Boolean;
		
		private var flvPlayer:FLVPlayback
		
		public function DemosPlayer()
		{
			setup();
		}
		
		public function set title (value:String):void
		{
			content.title_mc.text = value;
		}
		
		
		public function set source (value:String):void
		{
			flvPlayer.source = '../video/'+value;
		}
		
		public function open ():void
		{
			if (isOpen) return;
			
			visible = true;
			_screen.visible = true;
			bg_mc.scaleY = 0.02;
			Tweener.addTween(bg_mc, {scaleX:1, time:0.25, transition:'easeOutExpo'});
			Tweener.addTween(bg_mc, {scaleY:1, time:0.2, transition:'easeOutCubic', delay:0.15});
			Tweener.addTween(content, {alpha:1, time:1.2, delay:0.2, onComplete:function():void{flvPlayer.play()}});
			
			isOpen = true;
		}
		
		public function close (evt:Event = null):void
		{
			if (!isOpen) return;
			
			_screen.visible = false;
			Tweener.addTween(content, {alpha:0, time:0.2});
			Tweener.addTween(bg_mc, {scaleY:0, time:0.3});
			Tweener.addTween(bg_mc, {scaleX:0, time:0.3, delay:0.2, onComplete:function():void{visible = false;}});
			
			flvPlayer.stop();
			flvPlayer.seek(0);
			
			isOpen = false;
			
		}
		
		private function setup():void
		{
			visible = false;
			
			flvPlayer = content.flvPlayer;
			
			flvPlayer.autoPlay = false;
			flvPlayer.playPauseButton = content.pp_mc;
			content.pp_mc.buttonMode = true;
			content.pp_mc.stop();
			
			content.seekBar_mc.progress_mc.scaleX = 0;
			content.seekBar_mc.loaded_mc.scaleX = 0;
			content.seekBar_mc.mouseChildren = false;
			content.seekBar_mc.buttonMode = true;
			content.seekBar_mc.addEventListener(MouseEvent.MOUSE_DOWN, onProgressMouseDown);
			
			flvPlayer.playheadUpdateInterval = 10;
			flvPlayer.addEventListener('playheadUpdate', onPlayheadUpdate);
			
			bg_mc.scaleX = bg_mc.scaleY = 0;
			content.alpha = 0;
			
			content.cls_mc.stop();
			content.cls_mc.addEventListener(MouseEvent.CLICK, close);
			
			_screen = new Sprite ();
			_screen.graphics.beginFill(0xFFFFFF, 0.5);
			_screen.graphics.drawRect(-1000, -1000, 2000, 2000);
			_screen.addEventListener(MouseEvent.CLICK, onScreenClick);
			addChildAt(_screen, 0);
		}
		
		private function onPlayheadUpdate (evt:Event):void
		{
			content.seekBar_mc.progress_mc.scaleX = flvPlayer.playheadPercentage / 100;
		}
		
		private function onProgressMouseDown (evt:MouseEvent):void
		{
			flvPlayer.seekPercent((content.seekBar_mc.mouseX/content.seekBar_mc.width)*100);
		}
		
		private function onScreenClick (evt:MouseEvent):void
		{
			//
		}
		
	}
}